package com.nobuyme.box.toy;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.util.StrokeTextView;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.Bind;
import butterknife.OnClick;

public class ToyManageActivity extends BaseActivity {
    @Bind(R.id.iv_manage_score)
    ImageView mIvManageScore;
    private MyCountDownTimer mc;
    @Bind(R.id.stv_manage_time)
    StrokeTextView mStvManageTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
        hideToolbar();
        initScore();
        mc = new MyCountDownTimer(60000, 1000);
        mc.start();
    }

    private void initScore() {
        Bitmap mBitmap = null;
        Account acc = AccountManager.getCurrentAccount();
        int id = acc.getId();
        String str = String.format(UrlCenter.AMAIN,id);
        if (TextUtils.isEmpty(str)) {
            Toast.makeText(ToyManageActivity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        mBitmap = CodeUtils.createImage(str, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        mIvManageScore.setImageBitmap(mBitmap);

    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, ToyManageActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_toy_manage, null);
    }



    @OnClick({R.id.iv_manage_score, R.id.iv_manage_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_manage_score:
//                ManageHomeActivity.start(getApplicationContext());
//                ToyManageListActivity.start(this);
                break;
            case R.id.iv_manage_back:
                if (mc != null) {
                    mc.cancel();
                }

                finish();
                break;
        }
    }


    /**
     * 继承 CountDownTimer 防范
     * <p>
     * 重写 父类的方法 onTick() 、 onFinish()
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            finish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mStvManageTime != null) {
                mStvManageTime.setText(millisUntilFinished / 1000 + "s");
            }

        }
    }

}
